package com.ncmmall.application.business.goods;

import com.ncmmall.domain.business.goods.brand.GoodsBrand;
import com.ncmmall.domain.business.goods.brand.GoodsBrandRepository;
import com.ncmmall.domain.business.goods.brand.QGoodsBrand;
import com.ncmmall.querychannel.service.QueryChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

/**
 * @author linxs
 * @Description: 商品品牌
 * @Date: 2020-04-10 17:26
 */
@Service
@Transactional
public class GoodsBrandApplication {

    @Autowired
    private GoodsBrandRepository brandRepository;

    @Autowired
    private QueryChannelService queryChannelService;

    /**
     * 添加
     *
     * @param brand
     */
    public void create(GoodsBrand brand) {
        if (brand.getStatus() == 0) {
            brand.setStatus(1);
        }
        brand.setCreateTime(new Timestamp(System.currentTimeMillis()));
        brandRepository.save(brand);
    }

    /**
     * 修改
     *
     * @param params
     */
    public void modify(GoodsBrand params) {

        GoodsBrand brand = queryChannelService.findOne(QGoodsBrand.goodsBrand.id.eq(params.getId()), GoodsBrand.class);
        brand.setName(params.getName());
        brand.setLogo(params.getLogo());
        brand.setNote(params.getNote());
        brand.setStatus(1);
        brandRepository.save(brand);

    }

    public void modifyByAdmin(GoodsBrand params) {
        GoodsBrand brand = queryChannelService.findOne(QGoodsBrand.goodsBrand.id.eq(params.getId()), GoodsBrand.class);
        brand.setName(params.getName());
        brand.setLogo(params.getLogo());
        brand.setNote(params.getNote());
        brandRepository.save(brand);
    }

    /**
     * 删除
     *
     * @param params
     */
    public void remove(GoodsBrand params) {
        GoodsBrand brand = queryChannelService.findOne(QGoodsBrand.goodsBrand.id.eq(params.getId()), GoodsBrand.class);
        brand.setStatus(5);
        brandRepository.save(brand);
    }


    /**
     * 申请上架审核
     *
     * @param brand
     */
    public void applyPutaway(GoodsBrand params) {
        GoodsBrand brand = queryChannelService.findOne(QGoodsBrand.goodsBrand.id.eq(params.getId()), GoodsBrand.class);
        brand.setStatus(2);
        brandRepository.save(brand);
    }

    /**
     * 启用
     *
     * @param brand
     */
    public void enabled(GoodsBrand params) {
        GoodsBrand brand = queryChannelService.findOne(QGoodsBrand.goodsBrand.id.eq(params.getId()), GoodsBrand.class);
        brand.setStatus(3);
        brandRepository.save(brand);
    }

    /**
     * 禁用
     *
     * @param brand
     */
    public void disabled(GoodsBrand params) {
        GoodsBrand brand = queryChannelService.findOne(QGoodsBrand.goodsBrand.id.eq(params.getId()), GoodsBrand.class);
        brand.setStatus(4);
        brand.setRecommend(false);
        brandRepository.save(brand);
    }

    /**
     * 批量申请上架
     *
     * @param transform
     */
    public void applysPutaway(List<GoodsBrand> transform) {
        for (GoodsBrand brand : transform) {
            applyPutaway(brand);
        }
    }

    /**
     * 审核
     *
     * @param params
     */
    public void process(GoodsBrand params) {

        GoodsBrand brand = queryChannelService.findOne(QGoodsBrand.goodsBrand.id.eq(params.getId()), GoodsBrand.class);
        brand.setStatus(params.getStatus());
        brand.setNote(params.getNote());
        brandRepository.save(brand);
    }

    /**
     * 推荐首页
     *
     * @param params
     */
    public void recommend(GoodsBrand params) {
        GoodsBrand brand = queryChannelService.findOne(QGoodsBrand.goodsBrand.id.eq(params.getId()), GoodsBrand.class);
        brand.setRecommend(params.isRecommend());
        brandRepository.save(brand);
    }
}
